<template>
  
  <div class="Login-box">
    <!-- 顶部 -->
    <van-nav-bar title="登录" left-text="返回" left-arrow @click-left="GoBack">
      <template #right>
        <!-- <van-icon name="search" size="18" /> -->
        <router-link to="/Reg"> <span class="Login-Reg">注册</span> </router-link>
      </template>
    </van-nav-bar>
    <van-form @submit="onSubmit">
    <van-field
      v-model="username"
      name="用户名"
      label="用户名"
      placeholder="用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <div class="password">
      <van-field
      v-model="password"
      :type="type"
      name="密码"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
  
    </div>
    
    <div style="margin: 16px" class="Login-sumbit">
      <van-button round block type="info" native-type="submit" :disabled="this.username && this.password ? false : true">登录</van-button>
    </div>
  </van-form>
  </div>
</template>

<script>

let eye = document.querySelector(".icon-close");
let eye_close = document.querySelector(".icon-open")
export default {
  name: "Login",
  data() {
    return {
      username: "",
      password: "",
      Boolean: false,
      type: "password"
    };
  },
  methods: {
    /* 点击提交 */
    onSubmit() {
      // console.log("submit", values);
      this.service({
        method: "post",
        url: "/user/login",
        data:`loginname=${this.username}&password=${this.password}`
      })
      .then((res) => {
        if (res.data.code == 200) {
          setTimeout (()=>{
            this.$toast.success ("登录成功")
          },1000)
          let token = res.data.data.token;
          let userid = res.data.data.userid;
          localStorage.setItem('token', token);
          localStorage.setItem('userid', userid)
          if (this.$route.query.path){
            this.$router.push(this.$route.query.path);
          } else {
            this.$router.push("/My")
          }

        } else if (res.data.code == 10010) {
          setTimeout(()=>{
            this.$toast.fail('该用户不存在！')
          })
        } else {
          this.$toast.fail ("密码错误！")
        }
      })
    },
    GoBack() {
      this.$router.go(-1);
      // console.log('111');
    },
  },
};
</script>

<style scoped>
.Login-Reg {
  color: #1989fa;
}

</style>